const { ethers } = require("hardhat");

async function main() {
    const [deployer] = await ethers.getSigners();

    const MessageFactory = await ethers.getContractFactory("Message");
    message = await MessageFactory.deploy();
    await message.waitForDeployment();
    console.log("Message deployed to:", message.target);

    // 部署 CurrencyBrokers
    const CurrencyBrokersFactory = await ethers.getContractFactory(
        "CurrencyBrokers"
    );
    currencyBrokers = await CurrencyBrokersFactory.deploy();
    await currencyBrokers.waitForDeployment();
    console.log("CurrencyBrokers deployed to:", currencyBrokers.target);

    // 部署 TaskEvaluation
    const TaskEvaluationFactory = await ethers.getContractFactory(
        "TaskEvaluation"
    );
    taskEvaluation = await TaskEvaluationFactory.deploy();
    await taskEvaluation.waitForDeployment();
    console.log("TaskEvaluation deployed to:", taskEvaluation.target);

    // 部署 HelperInformation
    const HelperInformationFactory = await ethers.getContractFactory(
        "HelperInformation"
    );
    helperInformation = await HelperInformationFactory.deploy();
    await helperInformation.waitForDeployment();
    console.log("HelperInformation deployed to:", helperInformation.target);

    // 部署 Task
    const TaskFactory = await ethers.getContractFactory("Task");
    task = await TaskFactory.deploy(
        message.target,
        currencyBrokers.target,
        taskEvaluation.target,
        helperInformation.target
    );
    await task.waitForDeployment();
    console.log("Task deployed to:", task.target);
    // 部署 FinishTask
    const FinishTaskFactory = await ethers.getContractFactory("FinishTask");
    finishTask = await FinishTaskFactory.deploy(task.target);
    await finishTask.waitForDeployment();
    console.log("FinishTask deployed to:", finishTask.target);

    const tx = await currencyBrokers
        .connect(host)
        .receive({ value: ethers.parseEther("1") });
    await tx.wait();
}

main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error);
        process.exit(1);
    });
